import flash.events.Event;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;

import CustomSprite;
import Button;
import Images;


class ResourceMenuEvent extends Event{
	public function new(customEventString:String){

	super(customEventString, true, false);

    }
}

class ResourceMenu extends GenericMenu
{
public var currentPlayer:CharacterStat;
public var PurchasePrincess:CustomSprite;
public var PurchasePrincessText:TextField;
public var PurchaseWriter:CustomSprite;
public var PurchaseWriterText:TextField;
public var PurchaseResearcher:CustomSprite;
public var PurchaseResearcherText:TextField;
public var Description:CustomSprite;
public var DescriptionText:TextField;
     public function new(inBackground:BitmapData,inPlayer:CharacterStat) {
     super(inBackground,640,420);
     
     y = 100;
     currentPlayer = inPlayer;
     PurchasePrincess = new CustomSprite();
     PurchasePrincess.graphics.beginFill(0xFFFFFF);
     PurchasePrincess.graphics.lineStyle(1);
     PurchasePrincess.graphics.drawRect(0,0,100,100);
     PurchasePrincess.graphics.endFill();
     PurchasePrincess.isHover = false;
     PurchasePrincess.buttonMode =true;
     
     PurchasePrincess.x =20;
     PurchasePrincess.y = 85;
    var PrincessPic = new CustomSprite();
    PrincessPic.graphics.beginBitmapFill(new Princess());
    PrincessPic.graphics.drawRect(0, 0, 25,25);
    PrincessPic.graphics.endFill();
    PrincessPic.resize(50,50);
    PurchasePrincess.addChild(PrincessPic);
    var VillianPic = new CustomSprite();
    VillianPic.graphics.beginBitmapFill(new Villian());
    VillianPic.graphics.drawRect(0, 0, 25,25);
    VillianPic.graphics.endFill();
    VillianPic.resize(50,50);
    VillianPic.x=50;
    PurchasePrincess.addChild(VillianPic);
    var HeroPic = new CustomSprite();
    HeroPic.graphics.beginBitmapFill(new Hero());
    HeroPic.graphics.drawRect(0, 0, 25,25);
    HeroPic.graphics.endFill();
    HeroPic.resize(50,50);
    HeroPic.x=25;HeroPic.y=49;
    PurchasePrincess.addChild(HeroPic);
      
     PurchasePrincessText = new TextField();
     PurchasePrincessText.htmlText = getPurchasePrincessText();
     PurchasePrincessText.x = 120;
     PurchasePrincessText.y = 78;
     PurchasePrincessText.autoSize = TextFieldAutoSize.LEFT;
     addChild(PurchasePrincessText);
     addChild(PurchasePrincess);
     PurchasePrincess.addEventListener(MouseEvent.CLICK, onPurchasePrincesssButtonClick);
     PurchasePrincess.addEventListener(MouseEvent.ROLL_OVER,onHoverPrincess);
     PurchasePrincess.addEventListener(MouseEvent.ROLL_OUT,onHoverOutResource);

     PurchasePrincess.setHoverText("");

     PurchaseWriter = new CustomSprite();
     PurchaseWriter.graphics.beginFill(0xFFFFFF);
     PurchaseWriter.graphics.lineStyle(1);
     PurchaseWriter.graphics.drawRect(0,0,100,100);
     PurchaseWriter.graphics.endFill();
     PurchaseWriter.graphics.beginBitmapFill(new Writer());
     PurchaseWriter.graphics.drawRect(0, 0, 100,100);
     PurchaseWriter.graphics.endFill();
     PurchaseWriter.isHover = false;
     PurchaseWriter.setHoverText("");
     PurchaseWriter.x =450;
     PurchaseWriter.y = 85;
     PurchaseWriter.buttonMode =true;
      
     PurchaseWriterText = new TextField();
     PurchaseWriterText.htmlText = getPurchaseWriterText();
     PurchaseWriterText.x = 553;
     PurchaseWriterText.y = 78;
     PurchaseWriterText.autoSize = TextFieldAutoSize.LEFT;
     addChild(PurchaseWriterText);
     addChild(PurchaseWriter);
     PurchaseWriter.addEventListener(MouseEvent.CLICK, onPurchaseWriterButtonClick);
     PurchaseWriter.addEventListener(MouseEvent.ROLL_OVER,onHoverWriter);
     PurchaseWriter.addEventListener(MouseEvent.ROLL_OUT,onHoverOutResource);


     PurchaseResearcher = new CustomSprite();
     PurchaseResearcher.graphics.beginFill(0xFFFFFF);
     PurchaseResearcher.graphics.lineStyle(1);
     PurchaseResearcher.graphics.drawRect(0,0,100,100);
     PurchaseResearcher.graphics.endFill();
     PurchaseResearcher.graphics.beginBitmapFill(new ResearchImage());
     PurchaseResearcher.graphics.drawRect(0, 0, 100,100);
     PurchaseResearcher.graphics.endFill();
     PurchaseResearcher.x = 230;
     PurchaseResearcher.y = 85;
     PurchaseResearcher.buttonMode =true;
      
     PurchaseResearcherText = new TextField();
     PurchaseResearcherText.htmlText = getPurchaseResearcherText();
     PurchaseResearcherText.x = 330;
     PurchaseResearcherText.y = 78;
     PurchaseResearcherText.autoSize = TextFieldAutoSize.LEFT;
     addChild(PurchaseResearcherText);
     addChild(PurchaseResearcher);
     PurchaseResearcher.addEventListener(MouseEvent.CLICK, onPurchaseResearcherButtonClick);
     PurchaseResearcher.addEventListener(MouseEvent.ROLL_OVER,onHoverResearcher);
     PurchaseResearcher.addEventListener(MouseEvent.ROLL_OUT,onHoverOutResource);


     Description = new CustomSprite();
     Description.graphics.beginFill(0xFFFFFF);
     Description.graphics.lineStyle(1);
     Description.graphics.drawRect(0,0,390,205);
     Description.graphics.endFill();
     Description.x =230;
     Description.y =200;
     
      
     DescriptionText = new TextField();
     DescriptionText.htmlText = "<font size='20'></font>";
     DescriptionText.x = 5;
     DescriptionText.y = 5;
     DescriptionText.width = 385;
     DescriptionText.wordWrap= true;
     DescriptionText.selectable = false;
     DescriptionText.autoSize = TextFieldAutoSize.LEFT;
     Description.addChild(DescriptionText);
     addChild(Description);


  }

  private function onHoverResearcher(event:MouseEvent)
  {
    DescriptionText.htmlText = getResearcherText();
  }
  private function onHoverPrincess(event:MouseEvent)
  {
    DescriptionText.htmlText = getPrincessText();
  }
  private function onHoverWriter(event:MouseEvent)
  {
    DescriptionText.htmlText = getWriterText();
  }
  private function onHoverOutResource(event:MouseEvent)
  {	
    DescriptionText.htmlText = "<font size='20'></font>";
  }

  public function getPurchasePrincessText():String
  {
    return "<font size='20'>Team</font>\n<font size='16'>Total: "+  currentPlayer.PrincessTotal+"\nMax: "+ currentPlayer.getTeamLimit() +"</font>";
  }

  private function getPurchaseWriterText():String
  {
    return "<font size='20'>Writer</font>\n<font size='16'>Total: "+  currentPlayer.WriterTotal +"</font>";
  }

  private function getPurchaseResearcherText():String
  {
    return "<font size='20'>Researcher</font>\n<font size='16'>Total: "+  currentPlayer.ResearcherTotal + "</font>";
  }

  private function getResearcherText():String
  {
     var text = "<font size = '20'><b><u>Fear - " + currentPlayer.getResearcherCost() + "</u></b>\nPut that money into R&D.  It makes everything cheaper. Well, almost everything.</font>";
     return text;
  }
  private function getPrincessText():String
  {
     var text = "<font size = '20'><b><u>Fear - " + currentPlayer.getTeamCost() + "</u></b>\nCoherst peasants into becoming pawns in your massive plans.  Increases fear by creating more kidnapings.";
     return text;
  }

  private function getWriterText():String
  {
     var text = "<font size = '20'><b><u>Fear - " + currentPlayer.getWriterCost() + "</u></b>\nTired of that same old kidnapping.  Mix it up a little, maybe this time the villain is an ancient monster, or better yet, an ancient monster from SPACE!  Increases fear per kidnapings.</font>";
     return text;
  }

  private function onPurchasePrincesssButtonClick(event:MouseEvent) {
      var cost = currentPlayer.getTeamCost();
      if(currentPlayer.Fear >= cost && currentPlayer.PrincessTotal < currentPlayer.getTeamLimit())
      {
	currentPlayer.PrincessTotal+=1;
	currentPlayer.HeroTotal+=1;
	currentPlayer.VillianTotal +=1;
	currentPlayer.Fear -= cost;
      }
      DescriptionText.htmlText = getPrincessText();
      PurchasePrincessText.htmlText = getPurchasePrincessText();
  }


  private function onPurchaseWriterButtonClick(event:MouseEvent) {
      var cost = currentPlayer.getWriterCost();
      if(currentPlayer.Fear >= cost)
      {
	currentPlayer.WriterTotal+=1;
	currentPlayer.Fear -= cost;
      }
      DescriptionText.htmlText = getWriterText();
      PurchaseWriterText.htmlText = getPurchaseWriterText();
  }

  private function onPurchaseResearcherButtonClick(event:MouseEvent) {
      var cost = currentPlayer.getResearcherCost();
      if(currentPlayer.Fear >= cost)
      {
	currentPlayer.ResearcherTotal+=1;
	currentPlayer.Fear -= cost;
      }
      DescriptionText.htmlText = getResearcherText();
      PurchaseResearcherText.htmlText = getPurchaseResearcherText();
  }
}